package com.example.controller;

import com.example.dto.UserReq;
import com.example.model.User;
import com.example.service.RedisService;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Api(value="用户接口",tags = "UserAPI")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisService<User> redisService;

    @RequestMapping("/index.html")
    public String main() throws Exception{
        return "user/index";
    }
    @RequestMapping("/list.html")
    public String list() throws Exception{
        return "user/list";
    }

    @ApiOperation(value="新增用户信息")
    @ApiImplicitParam(dataType = "User",name = "user",value="用户信息",required = true)
    @RequestMapping(value="/save",method = RequestMethod.POST)
    @ResponseBody
    public int save(@RequestBody User user){
        return userService.save(user);
    }

    @ApiOperation(value = "更新用户信息")
    @ApiImplicitParam(dataType = "User",name = "user",value="用户信息",required = true)
    @RequestMapping(value="/update",method = RequestMethod.PUT)
    @ResponseBody
    public int update(@RequestBody User user){
        return userService.update(user);
    }

    @ApiOperation(value = "删除用户信息")
    @RequestMapping(value="/delete/{userId}",method = RequestMethod.DELETE)
    @ResponseBody
    public int delete(@PathVariable Integer userId){
        return userService.delete(userId);
    }

    @ApiOperation(value = "查询单个用户信息")
    @RequestMapping(value="/get/{userId}",method = RequestMethod.GET)
    @ResponseBody
    public User getUserById(@PathVariable("userId") Integer userId){
        return userService.getUserById(userId);
    }

    @ApiOperation(value = "查询单个用户信息-使用缓存版")
    @RequestMapping(value="/get/v2/{userId}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getUserByIdV2(@PathVariable("userId") Integer userId){
        Map<String,Object> result = new HashMap<>();
        User user = null;
        String key = "usr:" + userId;
        if(redisService.exists(key)){
            user = redisService.get(key);
            result.put("message", "我使用缓存了");
        }else{
            user = userService.getUserById(userId);
            redisService.set(key, user);
            result.put("message", "我查询数据库了");
        }
        result.put("user", user);
        return result;
    }

    @ApiOperation(value = "查询用户信息")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo getAllUsers(UserReq userReq,
                                @RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum,
                                @RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
        return userService.findAll(userReq, pageNum, pageSize);
    }
}
